package com.qingcheng.consumer;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.qingcheng.pojo.goods.Goods;
import com.qingcheng.pojo.goods.Sku;
import com.qingcheng.service.goods.SpuService;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;
import org.springframework.beans.factory.annotation.Value;

import java.io.File;
import java.util.List;

//商品详情页删除
@Service
public class PageDeleteMessageConsumer implements MessageListener {
    @Value("${pagePath}")
    private String pagePath;

    @Reference
    private SpuService spuService;

    public void onMessage(Message message) {
        String spuId = new String(message.getBody());
        Goods goods = spuService.findGoodsById(spuId);
        List<Sku> skuList = goods.getSkuList();

        File dir = new File(pagePath);
        if (dir.exists()){
            File[] files = dir.listFiles();
            for (File file : files) {
                System.out.println(file.getName());
                for (Sku sku : skuList) {
                    if(file.getName().contains(sku.getId())){
                        file.delete();  //删除页面
                        break;
                    }
                }
            }
        }
    }
}
